##!/usr/bin/env python3
## -*- coding: utf-8 -*-

from flask import Flask, request, render_template_string, redirect
import logging
import sys

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(levelname)s] %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout),
        logging.FileHandler('app.log')
    ]
)

logger = logging.getLogger(__name__)

# 初始化Flask应用
app = Flask(__name__)

# 简单的HTML表单模板
form_template = """
<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>访问码验证</title>
    <style>
      /* 重置边距和内边距 */
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      /* 页面样式 */
      body {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        background: linear-gradient(135deg, #74ebd5, #9face6);
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      /* 表单样式 */
      form {
        background-color: white;
        padding: 30px 40px;
        border-radius: 10px;
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        width: 100%;
        max-width: 450px;
        text-align: center;
      }

      h1 {
        font-size: 24px;
        color: #333;
        margin-bottom: 20px;
        font-weight: 600;
        line-height: 1.5;
      }

      /* 输入框样式 */
      input[type="text"] {
        width: 100%;
        padding: 12px;
        margin: 10px 0;
        border: 2px solid #ccc;
        border-radius: 8px;
        font-size: 16px;
        color: #333;
        outline: none;
        transition: border-color 0.3s ease;
      }

      input[type="text"]:focus {
        border-color: #007BFF;
      }

      /* 提交按钮样式 */
      input[type="submit"] {
        width: 100%;
        padding: 12px;
        background-color: #007BFF;
        color: white;
        border: none;
        border-radius: 8px;
        font-size: 16px;
        cursor: pointer;
        transition: background-color 0.3s ease;
      }

      input[type="submit"]:hover {
        background-color: #0056b3;
      }

      /* 占位符文本样式 */
      input::placeholder {
        color: #aaa;
        font-style: italic;
      }

      /* 响应式设计 */
      @media (max-width: 600px) {
        h1 {
          font-size: 20px;
        }

        form {
          padding: 25px;
        }

        input[type="text"], input[type="submit"] {
          padding: 10px;
        }
      }

    </style>
  </head>
  <body>
      <form method="post" action="{{ url_for('verify_code') }}">
      <h1>《长沙日历》多语种有声电子书<br>线上发布</h1>
      <input type="text" name="access_code" placeholder="请输入启动密码" required>
      <input type="submit" value="提交">
      {% if error_message %}
      <p style="color: red; margin-top: 10px;">{{ error_message }}</p>
      {% endif %}
    </form>
  </body>
</html>
"""

# 验证页面
@app.route('/', methods=['GET', 'POST'])
def verify_code():
    logger.info("检查请求方法: %s", request.method)
    if request.method == 'POST':
        access_code = request.form.get('access_code')
        logger.info(f"访问尝试，使用代码: {access_code}")
        
        if access_code == '418':
            logger.info("验证成功，重定向到目标网址")
            return redirect("https://duba.changshalib.cn/")
        else:
            logger.warning(f"无效的访问代码: {access_code}")
            error_message = "启动密码错误，请重试。"
            return render_template_string(form_template, error_message=error_message)
    
    logger.info("页面被访问")
    return render_template_string(form_template)

if __name__ == '__main__':
    app.run(debug=False)